package cn.jbolt.ec.order;

import cn.jbolt.base.JBoltBaseService;
import cn.jbolt.common.model.BecMemberCoupon;
import com.jfinal.kit.Kv;

import java.util.List;

/**
 * 充值送券
 *
 */
public class MemberCouponService extends JBoltBaseService<BecMemberCoupon> {
    private BecMemberCoupon dao = new BecMemberCoupon().dao();
    @Override
    protected BecMemberCoupon dao(){
        return dao;
    }

    public int append(int member_id, int retailer_id,
                      int face_value,long expired){

        BecMemberCoupon rec = dao();
        rec.setFaceValue(face_value);
        rec.setMemberId(member_id);
        rec.setRetailerId(retailer_id);
        rec.setExpired(expired);
        rec.setUsed(false);
        if(rec.save()){
            return rec.getId();

        }

        return 0;

    }

    public int pay(int id){
        BecMemberCoupon rec = findById(id);
        if(rec!=null && !rec.getUsed()){
            rec.setUsed(true);
            if(rec.update()) return 0;

        }
        return 1; // 使用代金券失败
    }
    /**
     * 获取

     */
    public List<BecMemberCoupon> get_by_retailer(int member_id,int retailer_id,int used_flag){
        Kv cond = Kv.by("member_id",member_id).set("retailer_id",retailer_id);
        if(used_flag==1) cond.set("used",false);
        if(used_flag==2) cond.set("used",true);
        return getCommonList(cond);

    }


}
